package com.ce.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.Logger;

public class FileOperate {
	public static Logger log = Logger.getLogger(FileOperate.class);
	private FileWriter writer;
	private String path;
	private File file;
	
	public FileOperate(String path) {
		this.path = path;
		log.info("file path:"+path);
	}

	public void prepare() throws Exception {
		this.file = new File(path);
		if(!file.exists()) {
			this.file.createNewFile();
		}
		writer = new FileWriter(this.file);
	}
	public void writelineProperties(String line) throws Exception {
		writer.write(line);
		writer.write("\r\n");
	}
	public void writeTagProperties(String line, String channel) throws Exception {
		writer.write("etv.tags."+channel+"=");
		writer.write(line);
		writer.write("\r\n");
	}
	public void writeListProperties(String line, String channel, String type, String page) throws Exception {
		writer.write("etv.list."+channel+"."+type+"."+page+"=");
		writer.write(line);
		writer.write("\r\n");
	}
	public void writeDetailProperties(String line, String id, String type) throws Exception {
		writer.write("etv.detail."+id+"."+type+"=");
		writer.write(line);
		writer.write("\r\n");
	}
	public void flush() throws Exception {
		writer.flush();
	}
	public void finish() {
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(writer != null)
					writer.close();   
            } catch (IOException e) {   
                e.printStackTrace();   
            } 
		}
	}
}
